/**
 * Project: complex.common
 * 
 * File Created at 2010-5-6
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.ymkyve.complex.common;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:Cookie的操作类
 * @author gaoliang
 * @date 2010-5-6
 * @version 1.0
 */
public class CookieUtils {

    public static void setCookie(HttpServletResponse response, String name, String value,String domain) {
        Cookie cookie = new Cookie(name, value);
        //设置60秒生存期，如果设置为负值的话，则为浏览器进程Cookie(内存中保存)，关闭浏览器就失效。
        cookie.setMaxAge(60000);
        //设置Cookie路径，不设置的话为当前路径(对于Servlet来说为request.getContextPath() + web.xml里配置的该Servlet的url-pattern路径部分)
        
        //cookie.setPath(path);
        if (domain != null) {
            cookie.setDomain(domain);
        }
        
        
        response.addCookie(cookie);
    }

    public static Cookie getCookie(HttpServletRequest request, String name) {
        //该方法可以读取当前路径以及“直接父路径”的所有Cookie对象，如果没有任何Cookie的话，则返回null
        Cookie[] cookies = request.getCookies();
        if (cookies == null)
            return null;
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie;
            }
        }
        return null;
    }

    public static void delCookie(HttpServletResponse response, String name) {
        //        Cookie cookie = getCookie(request,name);
        //        cookie.setMaxAge(0);
        Cookie cookie = new Cookie(name, null);
        //设置为0为立即删除该Cookie
        cookie.setMaxAge(0);
        //删除指定路径上的Cookie，不设置该路径，默认为删除当前路径Cookie
        cookie.setPath("/test/test2");
        response.addCookie(cookie);
    }

    public static void updateCookie(HttpServletRequest request, HttpServletResponse response,
                                    String name, String value) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null)
            return;
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                cookie.setValue(value);
                response.addCookie(cookie);
                break;
            }
        }
    }

    /**
     * 创建cookie
     * 
     * @param response 回应
     * @param nameValues 存入cookie的键值对
     * @param days 设置cookie的有效期
     */
    public static void createCookie(HttpServletResponse response,
                                    Hashtable<String, String> nameValues, int days) {
        Set<String> set = nameValues.keySet();
        Iterator<String> it = set.iterator();
        for (; it.hasNext();) {
            String name = (String) it.next();
            String value = (String) nameValues.get(name);
            // 生成新的cookie
            Cookie cookie = new Cookie(name, value);
            // 设置有效日期
            cookie.setMaxAge(days * 24 * 60 * 60);
            // 设置路径（默认）
            cookie.setPath("/");
            // 把cookie放入响应中
            response.addCookie(cookie);
        }
    }

    /**
     * 读取Cookie
     * 
     * @param request
     * @return Hashtable 返回cookie的键值对
     */
    public static Hashtable<String, String> getCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Hashtable<String, String> cookieHt = new Hashtable<String, String>();
        if (cookies.length > 0) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                cookieHt.put(cookie.getName(), cookie.getValue());
            }
        }
        return cookieHt;
    }

    /**
     * 修改cookie中指定键的值
     * 
     * @param request
     * @param name 指定的键
     * @param value 值
     */
    public static void setCookieValueByName(HttpServletRequest request, String name, String value) {
        Cookie[] cookies = request.getCookies();
        if (cookies.length > 0) {
            for (int i = 0; i > cookies.length; i++) {
                if (name.equalsIgnoreCase(cookies[i].getName())) {
                    cookies[i].setValue(value);
                }
            }
        }
    }

    /**
     * 得到指定键的值
     * 
     * @param request
     * @param name 指定的键
     * @return String 值
     */
    public static String getCookieValueByName(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        String resValue = "";
        if (cookies.length > 0) {
            for (int i = 0; i > cookies.length; i++) {
                if (name.equalsIgnoreCase(cookies[i].getName())) {
                    resValue = cookies[i].getValue();
                }
            }
        }
        return resValue;
    }

    /**
     * 销毁cookie
     * 
     * @param request
     * @param response
     */
    public static void deletesCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                // 销毁
                cookie.setMaxAge(0);
                response.addCookie(cookie);
            }
        }
    }

}
